<?php

namespace App\Entity\Sale;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Base\Annotation\Common;
use App\Entity\Shop\ShopCategory;
use App\Repository\Sale\SaleJuanRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass=SaleJuanRepository::class)
 */
class SaleJuan
{
    /**
     * @Common(cnName="#",listType="text",editType="text")
     * @Groups({"sale","juan"})
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @Common(cnName="名称",listType="text",editType="text",width="300")
     * @Groups({"sale","juan"})
     * @ORM\Column(type="string", length=255)
     */
    private $title;

    /**
     * @Common(cnName="说明",listType="text",editType="text",width="")
     * @Groups({"sale","juan"})
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $intro;

    /**
     * @Common(cnName="抵扣",listType="text",editType="text",width="110")
     * @Groups({"sale","juan"})
     * @ORM\Column(type="float")
     */
    private $cut;

    /**
     * @Common(cnName="满X可用",listType="text",editType="text",width="110")
     * @Groups({"sale","juan"})
     * @ORM\Column(type="float")
     */
    private $minUse;

    /**
     * @Common(cnName="有效期",listType="text",editType="text",width="110")
     * @Groups({"sale","juan"})
     * @ORM\Column(type="integer")
     */
    private $expireDays;

    /**
     * @Common(cnName="开始时间",listType="text",editType="datetime",width="180")
     * @Groups({"sale","juan"})
     * @ORM\Column(type="datetime")
     */
    private $startAt;

    /**
     * @Common(cnName="结束时间",listType="text",editType="datetime",width="180")
     * @Groups({"sale","juan"})
     * @ORM\Column(type="datetime")
     */
    private $endAt;

    /**
     * @Common(cnName="目录限制",listType="text",editType="text",width="")
     * @Groups({"prod","product"})
     * @ORM\ManyToMany(targetEntity=ShopCategory::class)
     */
    private $categories;

    /**
     * @ORM\OneToMany(targetEntity=SaleJuanLog::class, mappedBy="juan", orphanRemoval=true)
     */
    private $logs;

    public function __construct()
    {
        $this->categories = new ArrayCollection();
        $this->logs = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }

    public function getIntro(): ?string
    {
        return $this->intro;
    }

    public function setIntro(?string $intro): self
    {
        $this->intro = $intro;

        return $this;
    }

    public function getCut(): ?float
    {
        return $this->cut;
    }

    public function setCut(float $cut): self
    {
        $this->cut = $cut;

        return $this;
    }

    public function getMinUse(): ?float
    {
        return $this->minUse;
    }

    public function setMinUse(float $minUse): self
    {
        $this->minUse = $minUse;

        return $this;
    }

    public function getExpireDays(): ?int
    {
        return $this->expireDays;
    }

    public function setExpireDays(int $expireDays): self
    {
        $this->expireDays = $expireDays;

        return $this;
    }

    public function getStartAt(): ?\DateTimeInterface
    {
        return $this->startAt;
    }

    public function setStartAt(\DateTimeInterface $startAt): self
    {
        $this->startAt = $startAt;

        return $this;
    }

    public function getEndAt(): ?\DateTimeInterface
    {
        return $this->endAt;
    }

    public function setEndAt(\DateTimeInterface $endAt): self
    {
        $this->endAt = $endAt;

        return $this;
    }

    /**
     * @return Collection|ShopCategory[]
     */
    public function getCategories(): Collection
    {
        return $this->categories;
    }

    public function addCategory(ShopCategory $category): self
    {
        if (!$this->categories->contains($category)) {
            $this->categories[] = $category;
        }

        return $this;
    }

    public function removeCategory(ShopCategory $category): self
    {
        if ($this->categories->contains($category)) {
            $this->categories->removeElement($category);
        }

        return $this;
    }

    /**
     * @return Collection|SaleJuanLog[]
     */
    public function getLogs(): Collection
    {
        return $this->logs;
    }

    public function addLog(SaleJuanLog $log): self
    {
        if (!$this->logs->contains($log)) {
            $this->logs[] = $log;
            $log->setJuan($this);
        }

        return $this;
    }

    public function removeLog(SaleJuanLog $log): self
    {
        if ($this->logs->contains($log)) {
            $this->logs->removeElement($log);
            // set the owning side to null (unless already changed)
            if ($log->getJuan() === $this) {
                $log->setJuan(null);
            }
        }

        return $this;
    }
}
